package com.banksoft.cps.controller.buy;

import com.banksoft.cps.bean.BuyCompany;
import com.banksoft.cps.bean.Company;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.service.BuyCompanyService;
import com.banksoft.cps.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by rongzhao.zhu on 2018/9/5.
 */
@Controller
@RequestMapping("/buy/company/")
public class BuyCompanyController extends ControllerConstants {

    @Autowired
    BuyCompanyService buyCompanyService;

    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(){
        BuyCompany companyUser = (BuyCompany) UserContext.getUser();
        BuyCompany company = buyCompanyService.findWarpperById(companyUser.getId());
        return toJSONString(createDataTip(company));
    }

    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(BuyCompany company, HttpServletRequest request){
        try {
            buyCompanyService.save(company);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
}
